package com.xingzhi.shortvideosharingplatform.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingzhi.shortvideosharingplatform.common.Result;
import com.xingzhi.shortvideosharingplatform.dto.AdminDTO;
import com.xingzhi.shortvideosharingplatform.dto.LoginResponse;
import com.xingzhi.shortvideosharingplatform.model.AdminUser;
import com.xingzhi.shortvideosharingplatform.properties.JwtProperties;
import com.xingzhi.shortvideosharingplatform.service.AdminUserService;
import com.xingzhi.shortvideosharingplatform.utils.JwtUtil;
import com.xingzhi.shortvideosharingplatform.utils.PasswordUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminUserController {

    @Resource
    private AdminUserService adminUserService;

    @Resource
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<LoginResponse> adminLogin(@RequestBody AdminUser adminUser) {
        try {
            String userName = adminUser.getUserName();
            String password = adminUser.getPassword();
            if (userName == null || password == null) {
                return Result.error(401, "用户名或密码不能为空");
            }
            LambdaQueryWrapper<AdminUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AdminUser::getUserName, userName);
            AdminUser user = adminUserService.getOne(queryWrapper);
            if (user == null) {
                return Result.error(401, "用户不存在");
            }
            if (!PasswordUtil.checkPassword(password, user.getPassword())) {
                return Result.error(401, "密码错误");
            }
            Map<String, Object> claims = new HashMap<>();
            claims.put("userId", user.getId());
            String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);
            return Result.success(new LoginResponse(token, token));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(504, "发生了未知错误");
        }
    }

    @GetMapping("/info")
    public Result<AdminDTO> adminInfo(HttpServletRequest request) {
        String token = request.getHeader(jwtProperties.getAdminTokenName());
        Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
        Long userId = claims.get("userId", Long.class);
        AdminUser adminUser = adminUserService.getById(userId);
        AdminDTO info = new AdminDTO();
        info.setUserId(adminUser.getId());
        info.setUserName(adminUser.getUserName());
        List<String> roles = new ArrayList<>();
        roles.add("R_SUPER");
        info.setRoles(roles);
        List<String> buttons = new ArrayList<>();
        buttons.add("B_CODE1");
        buttons.add("B_CODE2");
        buttons.add("B_CODE3");
        info.setButtons(buttons);
        return Result.success(info);
    }

    @GetMapping("/list")
    public Result<List<AdminUser>> adminList(
            @RequestParam(value = "query", defaultValue = "") String query,
            @RequestParam(value = "pagenum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ) {
        LambdaQueryWrapper<AdminUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AdminUser::getUserName, query);
        Page<AdminUser> page = new Page<>(pageNum, pageSize);
        Page<AdminUser> adminUserPage = adminUserService.page(page, queryWrapper);
        return Result.success(adminUserPage.getRecords());
    }

}
